/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package openones.ipresenter.android.view;

import java.util.Timer;
import java.util.TimerTask;

import openones.ipresenter.android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * The first splash screen when the application starts.
 * @author OOG member
 */
public class WelcomeActivity extends Activity {
    /**
     * The tvPressAnyKey blinks every 500 miliseconds.
     */
    private static final int TIME_SINCE_LAST_BLINKING = 500;
    
    /**
     * The "Press any key to continue" TextView.
     * Used in twinkle effect. 
     */
    private TextView tvPressAnyKey;
    
    /**
     * Used to set tvPressAnyKey twinkle.
     */
    private Timer mTimer;
    
    /**
     * Called when the activity is first created.
     * @param savedInstanceState Information about the last state.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        
        tvPressAnyKey = (TextView) findViewById(R.id.welcome_tvPressAnyKey);
        startTimer();
    }
    
    /**
     * Start twinkling.
     */
    public void startTimer() {
        mTimer = new Timer();
        if (mTimer != null) {
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    WelcomeActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            if (tvPressAnyKey.isShown()) {
                                tvPressAnyKey.setVisibility(View.INVISIBLE);
                            } else {
                                tvPressAnyKey.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                }
                
            }, 0, TIME_SINCE_LAST_BLINKING);
        }
        
    }
    
    /**
     * Stop the timer.
     * This method is call when the user goes to Main Menu.
     */
    public void stopTimer() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer.purge();
            mTimer = null;
        }
    }
    
    /**
     * Press any key to continue.
     * Go to MainMenuActivity.
     * @param keyCode Indicate which key is up.
     * @param event Key and button event.
     * @return True if the event was handled.
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Intent intent = new Intent(WelcomeActivity.this, MainMenuActivity.class);
        startActivity(intent);
        finish();
        return true;
    }
    
    /**
     * Touch anywhere to continue.
     * Go to MainMenuActivity.
     * @param event Report movement event.
     * @return True if the event was handled.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(WelcomeActivity.this, MainMenuActivity.class);
        startActivity(intent);
        finish();
        return super.onTouchEvent(event);
    }
    
    /**
     * Called when the activity is destroyed. It is popped out of the Back Stack.
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    /**
     * Used to get the mTimer.
     * @return mTimer.
     */
    public Timer getmTimer() {
        return mTimer;
    }

}